package com.example.qzword.demos.controller;

import com.example.qzword.demos.model.Student;
import com.example.qzword.demos.service.Service01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class Controller01 {
    @Autowired
    Service01 service01;

    @GetMapping("/stu")
    public List<Student> getAllUsers(){
        return service01.list();
    }


    @GetMapping("/stu/{id}")
    public Student getUserById(@PathVariable("id") Integer id){
        Student student = service01.getById(id);
        return student;
    }



    @PostMapping("/stu")
    public String addUser(@RequestBody Student student){
        if (service01.save(student)){
            return "插入用户成功！";
        }
        else return "插入用户失败";
    }



    @PutMapping("/stu")
    public String updateUser(@RequestBody Student student){
        if (service01.updateById(student)){
            return "更新用户成功！";
        }
        else return "更新用户失败";
    }


    @DeleteMapping("/stu")
    public String deleteUserById(Integer id){
        if (service01.removeById(id)){
            return "删除用户成功！";
        }
        else return "删除用户失败！";
    }

}
